<?php

class DatabaseSeeder extends Seeder {

    public function run()
    {
        $this->call('LawyerTableSeeder');

        $this->command->info('Lawyer table seeded!');
    }

}

/**
 * 清理律师的数据
 * - 执业年限
 * - 认证状态
 * 
 * 已经没有用的列：is_verified
 */
class LawyerTableSeeder extends Seeder {

    public function run()
    {
        // 1. 处理律师的执业年限
        $results = DB::select("select user_id, practice_period, license from lawyer");
        foreach($results as $lawyer) {
            $practice_period = $this->getPracticePeriod($lawyer);
            if ($practice_period) {
                DB::update('update lawyer set practice_period = ? where user_id = ?', array($practice_period, $lawyer->user_id));
            }
    	}

        // 2. 处理律师的审核状态
        $results = DB::select("select id, is_verified from user");
        foreach($results as $user) {
            if ($user->is_verified) {
                DB::update('update user set status = ? where id = ?', array(User::STATUS_VERIFIED_LAWYER, $user->id));
            }
        }

        // 3. 处理用户的上传图像的路径
        $results = DB::select("select id, avatar from user where avatar is not null and avatar != ''");
        foreach($results as $user) {
            $avatar = str_replace("http://www.wdlst.com/","",$user->avatar);
            DB::update('update user set avatar = ? where id = ?', array($avatar, $user->id));
        }

        // 4. 处理post_extras
        // $results = DB::select("delete from post_extras where post_id <> any(select id from post)");

    }

    /**
     * 律师证号的格式： 13706201310434501
     * 第2-5位是地区编号，第6-9位是从事律师工作起始时间，后面是律师事务所和律师的编号
     * 可以接受的执业年限：(1to3|3to5|5to10|10
     */
    private function getPracticePeriod($lawyer) {

        // 第一步：通过律师证号来得到执业年限
        $pattern1 = '/^[0-9]{5}((19|20)[0-9]{2})[0-9]{6}/';
        $practice_period = null;
        $period = null;
        if (preg_match($pattern1, $lawyer->license, $matches)) {
            $year = $matches[1];
            $period = 2016 - (int)$year + 1;
        }

        // 第二步：把类似"2年"这样的记录变换一下
        $pattern2 = '/^(\d+)年/';
        if (preg_match($pattern2, $lawyer->practice_period, $matches)) {
            if ($matches[1] == "1986") {
                $period = 10;
            } else {
                $period = (int)$matches[1];
            }
        }

        // 第三步：把类似"三年"这样的记录变换一下
        $pattern3 = '/^(.*)年/';
        if (preg_match($pattern3, $lawyer->practice_period, $matches)) {
            if ($matches[1] == '三') {
                $period = 3;
            }
            if ($matches[1] == '四') {
                $period = 4;
            }
            if ($matches[1] == '八') {
                $period = 8;
            }
            if ($matches[1] == '十二') {
                $period = 12;
            }
            if ($matches[1] == '十四') {
                $period = 14;
            }
            if ($matches[1] == '十五') {
                $period = 15;
            }
            if ($matches[1] == '十六') {
                $period = 16;
            }
            if ($matches[1] == '二十') {
                $period = 20;
            }
        }

        if ($period) {
            if ($period >= 1 && $period <= 3) {
                $practice_period = '1to3';
            } else if ($period >= 3 && $period < 5) {
                $practice_period = '3to5';
            } else if ($period >= 5 && $period < 10) {
                $practice_period = '5to10';
            } else if ($period >= 10) {
                $practice_period = '10';
            }
        }

        return $practice_period;
    }

}